package com.netrust.cmsx.entity.module.sys;

import static javax.persistence.GenerationType.IDENTITY;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.Length;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.google.common.collect.Lists;
import com.netrust.cmsx.core.config.Global;
import com.netrust.cmsx.core.persist.DataEntity;
import com.netrust.cmsx.utils.collections.CollectionUtils;




/**
 * 
 * @author lazite
 *
 */
@Entity
@Table(name = "sys_user")
@JsonIgnoreProperties(value={"hibernateLazyInitializer"})
public class User extends DataEntity<User>{

	private static final long serialVersionUID = 1L;
	
	@Column(name = "login_name")
	private String loginName;// 登录名
	@Column(name = "password")
	private String password;// 密码
	@Column(name = "no")
	private String no;		// 工号
	@Column(name = "name")
	private String name;	// 姓名
	@Column(name = "email")
	private String email;	// 邮箱
	@Column(name = "phone")
	private String phone;	// 电话
	@Column(name = "mobile")
	private String mobile;	// 手机
	@Column(name = "user_type")
	private String userType;// 用户类型
	@Column(name = "login_ip")
	private String loginIp;	// 最后登陆IP
	@Column(name = "login_date")
	private Date loginDate;	// 最后登陆日期
	@Column(name = "login_flag")
	private boolean loginFlag;// 是否允许登陆
	@Column(name = "del_flag")
	private boolean delFlag;// 是否删除
	@Column(name = "photo")
	private String photo;	// 头像
	@Column(name = "remarks")
	private String remarks;	// 备注
	
	@Transient
	private String oldLoginName;// 原登录名
	@Transient
	private String newPassword;	// 新密码
	@Transient
	private String oldLoginIp;	// 上次登陆IP
	@Transient
	private Date oldLoginDate;	// 上次登陆日期
	
	@Transient
	//@JSONField(serialize=false)
	private List<String> roleIdList;	// 上次登陆日期
	
	@JSONField(serialize=false)
	@ManyToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
    @JoinTable(
            name="sys_user_role",
            joinColumns=@JoinColumn(name="user_id"),
            inverseJoinColumns=@JoinColumn(name="role_id")
    )
	private List<Role> roles = Lists.newArrayList(); // 拥有角色列表

	public User() {
		super();
		this.loginFlag = true;
	}
	
	public User(String id){
		super(id);
	}

	public User(String id, String loginName){
		super(id);
		this.loginName = loginName;
	}
	
	public String getPhoto() {
		return photo;
	}

	public void setPhoto(String photo) {
		this.photo = photo;
	}

	public boolean getLoginFlag() {
		return loginFlag;
	}

	public void setLoginFlag(boolean loginFlag) {
		this.loginFlag = loginFlag;
	}
	
	public boolean getDelFlag() {
		return delFlag;
	}

	public void setDelFlag(boolean delFlag) {
		this.delFlag = delFlag;
	}

	public String getId() {
		return id;
	}

	@Length(min=1, max=100, message="登录名长度必须介于 1 和 100 之间")
	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	@JsonIgnore
	@Length(min=1, max=100, message="密码长度必须介于 1 和 100 之间")
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Length(min=1, max=100, message="姓名长度必须介于 1 和 100 之间")
	public String getName() {
		return name;
	}
	
	@Length(min=1, max=100, message="工号长度必须介于 1 和 100 之间")
	public String getNo() {
		return no;
	}

	public void setNo(String no) {
		this.no = no;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Email(message="邮箱格式不正确")
	@Length(min=0, max=200, message="邮箱长度必须介于 1 和 200 之间")
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}
	
	@Length(min=0, max=200, message="电话长度必须介于 1 和 200 之间")
	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	@Length(min=0, max=200, message="手机长度必须介于 1 和 200 之间")
	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	
	@Length(min=0, max=100, message="用户类型长度必须介于 1 和 100 之间")
	public String getUserType() {
		return userType;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}


	public String getLoginIp() {
		return loginIp;
	}

	public void setLoginIp(String loginIp) {
		this.loginIp = loginIp;
	}

	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	public Date getLoginDate() {
		return loginDate;
	}

	public void setLoginDate(Date loginDate) {
		this.loginDate = loginDate;
	}

	public String getOldLoginName() {
		return oldLoginName;
	}

	public void setOldLoginName(String oldLoginName) {
		this.oldLoginName = oldLoginName;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public String getOldLoginIp() {
		if (oldLoginIp == null){
			return loginIp;
		}
		return oldLoginIp;
	}

	public void setOldLoginIp(String oldLoginIp) {
		this.oldLoginIp = oldLoginIp;
	}

	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	public Date getOldLoginDate() {
		if (oldLoginDate == null){
			return loginDate;
		}
		return oldLoginDate;
	}

	public void setOldLoginDate(Date oldLoginDate) {
		this.oldLoginDate = oldLoginDate;
	}

	@JsonIgnore
	public List<Role> getRoles() {
		return roles;
	}
	
	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}
	
	public String getRemarks() {
		return remarks;
	}

	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}

	@JsonIgnore
	public List<String> getRoleIdList() {
		
		if(CollectionUtils.isNotEmpty(roles)){
			List<String> roleIdList = Lists.newArrayList();
			for (Role role : roles) {
				roleIdList.add(role.getId());
			}
			this.roleIdList=roleIdList;
		}
		
		return roleIdList;
	}

	public void setRoleIdList(List<String> roleIdList) {
//		roles = Lists.newArrayList();
//		for (String roleId : roleIdList) {
//			Role role = new Role();
//			role.setId(roleId);
//			roles.add(role);
//		}
		this.roleIdList=roleIdList;
	}
	
	/**
	 * 用户拥有的角色名称字符串, 多个角色名称用','分隔.
	 */
	@JSONField(serialize=false)
	public String getRoleNames() {
		return CollectionUtils.extractToString(roles, "name", ",");
	}
	
	public boolean isAdmin(){
		return isAdmin(this.id);
	}
	
	public static boolean isAdmin(String id){
		return id != null && "1".equals(id);
	}

	@Override
	public String toString() {
		return "User [loginName=" + loginName + ", password=" + password + ", no=" + no + ", name=" + name + ", email="
				+ email + ", phone=" + phone + ", mobile=" + mobile + ", userType=" + userType + ", loginIp=" + loginIp
				+ ", loginDate=" + loginDate + ", loginFlag=" + loginFlag + ", delFlag=" + delFlag + ", photo=" + photo
				+ ", remarks=" + remarks + ", oldLoginName=" + oldLoginName + ", newPassword=" + newPassword
				+ ", oldLoginIp=" + oldLoginIp + ", oldLoginDate=" + oldLoginDate + ", roleIdList=" + roleIdList + "]";
	}

	
}
